package com.course.chapter2;

import java.util.concurrent.TimeUnit;

public class VolatileDemo1 {
    static volatile int index = 1;
    final static  int MAX =5;

    public static void main(String[] args) {
        new Thread(()->{
            int local_index = index;
            while (local_index<MAX){
                if(local_index!=index){
                    System.out.println(String.format("reader:[%S]",index));
                    local_index = index;
                }
            }
        }).start();

        new Thread(()->{
            int local_index = index;
            while (local_index<MAX){
                System.out.println(String.format("update:[%S]",index++));
                local_index =index;
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
